package org.itheima.googleplay.ui.activity;

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;

/**
 * @包名: org.itheima.googleplay.ui.activity
 * @类名: BaseActivity
 * @作者: 肖琦
 * @创建时间 : 2015-3-23 下午3:41:19
 * 
 * @描述: TODO
 * 
 * @SVN版本: $Rev: 18 $
 * @更新人: $Author: xq $
 * @更新时间: $Date: 2015-03-23 16:00:13 +0800 (星期一, 23 三月 2015) $
 * 
 * @更新描述:
 */
public class BaseActivity extends ActionBarActivity
{
	/**
	 * 存储全局的activity
	 */
	private static List<Activity>	mActivities	= new LinkedList<Activity>();

	/**
	 * 当前前台的activity
	 */
	private static Activity			mForegroundActivity;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		synchronized (mActivities)
		{
			mActivities.add(this);
		}

		initView();
		initActionBar();
		initData();
	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();

		synchronized (mActivities)
		{
			mActivities.remove(this);
		}
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		mForegroundActivity = this;
	}

	@Override
	protected void onPause()
	{
		super.onPause();
		mForegroundActivity = null;
	}

	public void exitApp()
	{
		// synchronized (mActivities)
		// {
		// for (Activity act : mActivities)
		// {
		// act.finish();
		// }
		// }

		// 线程安全的操作
		ListIterator<Activity> iterator = mActivities.listIterator();
		while (iterator.hasNext())
		{
			Activity next = iterator.next();
			next.finish();
		}

	}

	/**
	 * 加载数据的方法，自己覆盖实现
	 */
	protected void initData()
	{
		// TODO Auto-generated method stub

	}

	/**
	 * 初始化ActionBar的方法，子类如果有ActionBar的初始化，自己覆盖实现
	 */
	protected void initActionBar()
	{
		// TODO Auto-generated method stub

	}

	/**
	 * 初始化View的方法，子类如果有View的初始化，自己覆盖实现
	 */
	protected void initView()
	{
		// TODO Auto-generated method stub

	}

	/**
	 * 获取前台activity
	 * 
	 * @return
	 */
	public Activity getForegroundActivity()
	{
		return mForegroundActivity;
	}

}
